package com.eric.r2d2.overview;

import static java.util.Arrays.asList;

import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.bson.Document;
import org.json.JSONException;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoException;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class Overview {

	public static void main(String[] args) throws MalformedURLException, JSONException {
		MongoDatabase db;
		MongoCollection<Document> coll1;
		MongoCollection<Document> coll2;
		MongoCollection<Document> r;
		MongoClient mc = null;
		
			try {
				mc = new MongoClient(new MongoClientURI("mongodb://aliyun_sem"));
			} catch (MongoException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			db = mc.getDatabase("seo");
	        coll1 = db.getCollection("seo_allsites");
	        coll2 = db.getCollection("scheduler");
	        
	        r = db.getCollection("reporting");
	        
	        AggregateIterable<Document> outlinks_count = coll1.aggregate(asList(
	                new Document("$group", new Document("_id", "1").append("sum", new Document("$sum", "$outlinks_count")))));
	        AggregateIterable<Document> ext_outlinks_count = coll1.aggregate(asList(
	                new Document("$group", new Document("_id", "1").append("sum", new Document("$sum", "$ext_outlinks_count")))));
	        AggregateIterable<Document> text_count = coll1.aggregate(asList(
	                new Document("$group", new Document("_id", "1").append("sum", new Document("$sum", "$text_count")))));
	        
	        Document report = new Document();
	        report.put("report_date", new SimpleDateFormat("yyyyMMdd").format(new Date()));
	        report.put("total_urls_crawled", coll1.count());
	        report.put("total_text_count", text_count.iterator().next().get("sum"));
	        report.put("total_urls_found", coll2.count());
	        report.put("total_outlinks", outlinks_count.iterator().next().get("sum"));
	        report.put("total_ext_outlinks", ext_outlinks_count.iterator().next().get("sum"));
	        
	        r.insertOne(report);
	        
	        System.out.println(report);
	}

}
